﻿@typeparam TItem where TItem : VrrpScript

<MDialog @bind-Value="Value"
         MaxWidth="600"
         Persistent>
    <MCard>
        <MCardTitle>
            <span class="text-h5">跟踪脚本配置</span>
            <MSpacer />
            <MButton Icon
                     OnClick="CloseDialog">
                <MIcon>mdi-close</MIcon>
            </MButton>
        </MCardTitle>

        <MCardText>
            <MRow>
                <MCol Cols="12">
                    <MTextField @bind-Value="Item.Name"
                                Label="脚本名称"
                                Outlined
                                Dense
                                Required
                                Rules="_requiredRules" />
                </MCol>
            </MRow>

            <MRow>
                <MCol Cols="12">
                    <MTextarea @bind-Value="Item.Script"
                               Label="脚本内容"
                               Outlined
                               Dense
                               Rows="3"
                               AutoGrow
                               Required
                               Rules="_requiredRules" />
                </MCol>
            </MRow>

            <MRow>
                <MCol Cols="12" Md="4">
                    <MTextField @bind-Value="Item.Interval"
                                TValue="int"
                                Label="执行间隔(秒)"
                                Type="number"
                                Outlined
                                Dense
                                Required
                                Rules="_requiredRules_int" />
                </MCol>
                <MCol Cols="12" Md="4">
                    <MTextField @bind-Value="Item.Weight"
                                TValue="int"
                                Label="权重"
                                Type="number"
                                Outlined
                                Dense
                                Required
                                Rules="_requiredRules_int" />
                </MCol>
                <MCol Cols="12" Md="4">
                    <MTextField @bind-Value="Item.Timeout"
                                Label="超时时间(秒)"
                                Type="number"
                                Outlined
                                Dense />
                </MCol>
            </MRow>
        </MCardText>

        <MCardActions>
            <MSpacer />
            <MButton Color="primary"
                     OnClick="SaveItem">
                <MIcon Left>mdi-content-save</MIcon>
                保存
            </MButton>
            <MButton Color="secondary"
                     OnClick="CloseDialog">
                取消
            </MButton>
        </MCardActions>
    </MCard>
</MDialog>

@code {
    [Parameter]
    public bool Value { get; set; }

    [Parameter]
    public EventCallback<bool> ValueChanged { get; set; }

    [Parameter]
    public TItem Item { get; set; }

    [Parameter]
    public EventCallback<TItem> ItemChanged { get; set; }

    [Parameter]
    public EventCallback<TItem> OnSave { get; set; }

    private List<Func<string, StringBoolean>> _requiredRules = new()
    {
        value => string.IsNullOrWhiteSpace(value) ? "必填字段" : true
    };

    private List<Func<int, StringBoolean>> _requiredRules_int = new()
    {
        value => value==0 ? "必填字段" : true
    };

    private async Task SaveItem()
    {
        await OnSave.InvokeAsync(Item);
        await CloseDialog();
    }

    private async Task CloseDialog()
    {
        Value = false;
        await ValueChanged.InvokeAsync(Value);
    }
}